/**
  * Created by wyc on 6/10/2017.
  */
package ninenineqs

object p03 {
  def nth_v1[T](n:Int, input:List[T]):T = {
    if (n >= 0 && n < input.length) input(n)
    else throw new NoSuchElementException
  }
  def nth_v2[T](n:Int, input:List[T]):T = {
      input.take(n + 1).last
  }
  def nth_v3[T](n:Int, input:List[T]):T = {
    (n, input) match {
      case (0, h::_) => h
      case (n, _::tail) if n > 0 => nth_v3(n-1, tail)
      case _  => throw new NoSuchElementException
    }
  }
}
